Module 1- Import Local CSVs

author: Radley Rigonan

This is an example of reading and importing .CSV files stored in your device onto Python. Comma separated values (.CSV) is a filetype that stores tabular data in plaintext. It is a widely standard format used in spreadsheets, data storage, and data exchange.

In this module, I will be using lbl.csv which can be downloaded from the following link: http://radwatch.berkeley.edu/sites/default/files/dosenet/lbl.csv


In [1]:
import csv      # module used for reading and converting .CSV files
import os       # module that enables local operating system dependent commands

Normally, you're file of interest is not located in the default working directory of Python. os.chdir() changes the working directory so we can open our file of interest.


In [2]:
filepath = 'C:/Users/Radley/Downloads/' # store file location as a string
filename = 'lbl.csv'                   # store the file name as a string

The following module will read the CSV with csv.reader and display the data in your console one row at a time. This display command inserts a comma and space between each data entry, effectively reprinting the .CSV in your Python console.


In [3]:
def printlocalCSV(filepath, filename):
    file = os.path.join(filepath,filename)      # join filepath/name into one string
    csvfile = open(file)                 # open the file with Python
    reader = csv.reader(csvfile)         # read the CSV with csv.reader
    for row in reader:                   # for every row in the CSV...
        print(', '.join(row))            
            # Display/print the CSV with a comma and space separating each element
    csvfile.close()                      # close the file

The following module is an example of reading a CSV and importing the data as variables in Python. In many circumstances, you will want to import the data so that it can be used in Python. In addition, I use csv.reader with different, more efficient syntax:


In [4]:
def importlocalCSV(filepath, filename):
    datetime = []               
    cpm = []
        # Set up an empty lists so you can place the data into it.
    line = 0                    
        # We want to ignore the 1st row of headers, so we set a variable to 
        # count the row we are iterating on.
        
    file = os.path.join(filepath,filename)
    with open(file) as csvfile:
        # It is good practice to use the with statement when dealing with files
        # because it automatically closes the file after the nested lines
        # This with command performs same function as csvfile = open(file)
        
        reader = csv.reader(csvfile)
        for row in reader:
            if line != 0:       
                # Conditional statement to ignore first row of headers
                datetime.append(row[0])     
                    # Append means ATTACH.  We are attaching data in the 1st 
                    # column to our list, row by row
                cpm.append(float(row[6]))   
                    # The command float records each element as a numerical 
                    # value rather than a character string.
            line = line + 1                 
                    # Progress iteration forward by adding +1 to line
    return datetime, cpm